//
//  AdvanceViewController.h
//  DebugDemo
//
//  Created by XFB on 2018/11/8.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface AdvanceViewController : UIViewController

@end

NS_ASSUME_NONNULL_END

/*
 //
 //  AdvanceViewController.m
 //  DebugDemo
 //
 //  Created by XFB on 2018/11/8.
 //  Copyright © 2018年 ZhouCheng. All rights reserved.
 //
 
 #import "AdvanceViewController.h"
 #import "AdvanceMVVMViewController.h"
 
 #import <JavaScriptCore/JavaScriptCore.h>
 #import <WebKit/WebKit.h>
 #import "PerformanceOptiomizationViewController.h"
 
 
 @interface AdvanceViewController () <WKNavigationDelegate, WKUIDelegate, WKScriptMessageHandler, UIScrollViewDelegate>
 
 @property (nonatomic, strong) WKWebView *wkWebView;
 
 @end
 
 @implementation AdvanceViewController
 
 - (void)viewDidLoad {
 [super viewDidLoad];
 
 self.view.backgroundColor = [UIColor whiteColor];
 
 /// WKWebViewConfiguration的基础配置属性
 //    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
 //    /// JS 调用原生 OC
 //    [config.userContentController addScriptMessageHandler:self name:@"iOSAppShare"];
 //    /// 这个需要引用第三方 避免循环引用weak的问题
 ////    [config.userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"iOSAppShare"];
 //
 //    _wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, NavBarHeight, ScreenW, ScreenH-TabbarHeight-NavBarHeight) configuration:config];
 //    _wkWebView.UIDelegate = self;
 //    _wkWebView.navigationDelegate = self;
 //    _wkWebView.scrollView.delegate = self;
 //
 //    NSString *urlString = @"https://www.jianshu.com/p/7c146774bdac";
 //    NSURL *currentURL = [NSURL URLWithString:urlString];
 //    [_wkWebView loadRequest:[NSURLRequest requestWithURL:currentURL]];
 //    [self.view addSubview:_wkWebView];
 
 /**
 添加scriptMessageHandler到所有的Frames中，则都可以通过
 window.webkit.messageHandlers.<name>.postMessage(<messageBody>)
 
 /// JS端
 window.webkit.messageHandlers.iOSAppShare.postMessage({body: 'sender message'});
 
 WKUserScript 可以用于JS交互，将其注入到userContentController中，可以将其进行交互
 XFB项目中
 NSString *signString = [NSString stringWithFormat:@"localStorage.setItem(\"iOSSign\",'%@');", [Settings LoginSign].Value];
 
 /// source：JS源代码
 /// injectionTime：JS注入的时间
 /// forMainFrameOnly：是否只注入main frame
 /// WKUserScriptInjectionTimeAtDocumentStart ---> 注入时机为document的元素生成之后,其他内容load之前.
 /// WKUserScriptInjectionTimeAtDocumentEnd ---> 注入时机为document全部load完成,任意子资源load完成之前.
 /// 如果想尽早注入cookie,在WKUserScriptInjectionTimeAtDocumentStart是完成即可
 - (instancetype)initWithSource:(NSString *)source injectionTime:(WKUserScriptInjectionTime)injectionTime forMainFrameOnly:(BOOL)forMainFrameOnly;
 
 WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:signString injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
 
 WKWebsiteDataStore *websiteDataStore
 iOS9以后才能用这个类，代表webView不同的数据类型，cookie、disk、memory caches、webSQL等
 
 //默认的存储
 + (WKWebsiteDataStore *)defaultDataStore;
 
 //暂时性的存储
 + (WKWebSiteDataStore *)nonPersistentDataStore;
 
 //是否是永久的存储
 @property (nonatomic, readonly, getter=isPersistent) BOOL persistent;
 
 //所有的web存储类型 ***
 + (NSSet<NSString *> *)allWebsiteDataTypes;
 
 //根据网页的类型获取数据
 - (void)fetchDataRecordsOfTypes:(NSSet<NSString *> *)dataTypes completionHandler:(void (^)(NSArray<WKWebsiteDataRecord *> *))completionHandler;
 
 //根据网页的类型删除数据
 - (void)removeDataOfTypes:(NSSet<NSString *> *)dataTypes forDataRecords:(NSArray<WKWebsiteDataRecord *> *)dataRecords completionHandler:(void (^)(void))completionHandler;
 
 //更具网页的类型和时间删除数据
 - (void)removeDataOfTypes:(NSSet<NSString *> *)websiteDataTypes modifiedSince:(NSDate *)date completionHandler:(void (^)(void))completionHandler;
 
 //删除缓存
 //指定要删除的web的类型，这里指定的是所有的web的类型
 NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
 
 //指定一个时间，从哪个时间段的缓存开始删除
 NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
 
 //执行删除代码
 [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
 //删除后执行操作
 }];

}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    if ([message.name isEqualToString:@"iOSAppShare"]) { /// 分享事件
        NSLog(@"%@", message.body);
    }
}


 // 决定导航的动作，通常用于处理跨域的链接能否导航。WebKit对跨域进行了安全检查限制，不允许跨域，因此我们要对不能跨域的链接单独处理。但是，对于Safari是允许跨域的，不用这么处理。
 // 这个是决定是否Request，在发送请求之前，决定是否跳转
 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
 
 // 决定是否接收响应
 // 这个是决定是否接收response
 // 要获取response，通过WKNavigationResponse对象获取
 - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
 
 // 当main frame的导航开始请求时，会调用此方法，开始加载时调用
 - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
 
 // 当main frame接收到服务重定向时，会回调此方法，接收到服务器跳转请求之后调用
 - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation
 
 // 当main frame开始加载数据失败时，会回调，页面加载失败时调用
 - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
 
 // 当main frame的web内容开始到达时，会回调，当内容开始返回时调用
 - (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation
 
 // 当main frame导航完成时，会回调，页面加载完成之后调用
 - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation
 
 // 当main frame最后下载数据失败时，会回调
 - (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
 
 // 这与用于授权验证的API，与AFN、UIWebView的授权验证API是一样的
 - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler
 
 // 当web content处理完成时，会回调
 - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView
 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    PerformanceOptiomizationViewController *vc = [[PerformanceOptiomizationViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
    //    AdvanceMVVMViewController *vc = [[AdvanceMVVMViewController alloc] init];
    //    [self.navigationController pushViewController:vc animated:YES];
}


@end

 */
